#include <iostream>
#include <memory>
#include <string>
#include "TcpServer.hpp"
#include "ServerCal.hpp"
#include "Log.hpp"

std::unique_ptr<ServerCal> calculator(new ServerCal());

void DefaultOnMessage(std::weak_ptr<Connection> conn){
    if(conn.expired()){
        return;
    }
    auto connection = conn.lock();

    // 1.处理接收缓冲区中的数据
    std::string response = calculator->Calculator(connection->Inbuffer());
   
    // 2.将应答存放到发送缓冲区中
    connection->AddOutbuffer(response);

    // 3.回调发送函数
    auto tcpserver = connection->_tcpserver_ptr.lock();
    tcpserver->sender(connection);
}

int main(){
    std::shared_ptr<TcpServer> reactor_svr(new TcpServer(DefaultOnMessage));
    reactor_svr->Init();
    reactor_svr->EventLoop();
    
    return 0;
}